package com.centanet.framework.base;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.StringRes;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.centanet.centalib.R;
import com.centanet.framework.widget.CProgressDialog;
import com.orhanobut.logger.Logger;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

/**
 * 基类Activity
 */
public abstract class AbstractActivity extends RxAppCompatActivity {

    public static final String TITLE_ACTIVITY = "TITLE_ACTIVITY";//页面标题
    protected static final int REQ_PERMISSION = 88;//权限请求
    /**
     * {@link Toolbar}
     */
    protected Toolbar toolbar;

    protected CProgressDialog loadingDialog;

    /**
     * 设置默认资源，不跟随系统字体大小
     *
     * @return Resources
     */
    @Override
    public Resources getResources() {//不跟随系统字体大小
        Resources resources = super.getResources();
        Configuration configuration = new Configuration();
        configuration.setToDefaults();
        resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        return resources;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(setLayoutId());
        preInit();
        findViews();
        initViews();
    }

    /**
     * 布局id
     *
     * @return layoutId
     */
    @LayoutRes
    protected abstract int setLayoutId();

    /**
     * 初始化view前的内容
     */
    protected void preInit(){

    }

    /**
     * findViewById
     */
    protected abstract void findViews();

    /**
     * view初始化
     */
    protected abstract void initViews();

    /**
     * 设置通用的toolbar,设置返回按钮,setUniversalToolbar只需调用一次
     */
    protected void setUniversalToolbar() {
        setUniversalToolbar("");
    }

    /**
     * 设置通用的toolbar，设置标题、默认添加返回按钮,setUniversalToolbar只需调用一次
     *
     * @param id 标题
     */
    protected void setUniversalToolbar(@StringRes int id) {
        setUniversalToolbar(getString(id));
    }

    /**
     * 设置通用的toolbar，设置标题、默认添加返回按钮,setUniversalToolbar只需调用一次
     *
     * @param toolbarTitle 标题
     */
    protected void setUniversalToolbar(CharSequence toolbarTitle) {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        String activityTitle = getIntent().getStringExtra(TITLE_ACTIVITY);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle(TextUtils.isEmpty(activityTitle) ? toolbarTitle : activityTitle);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    /**
     * 设置ToolbarTitle
     *
     * @param title CharSequence
     */
    protected void setToolbarTitle(CharSequence title) {
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle(title);
        }
    }

    /**
     * 添加默认返回事件，finish当前页面
     *
     * @param item MenuItem
     * @return boolean
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {//返回键关闭页面
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * Snackbar统一显示入口
     */
    protected void snack(@StringRes int id) {
        snack(getString(id));
    }

    /**
     * Snackbar统一显示入口
     *
     * @param text 显示内容
     */
    protected void snack(CharSequence text) {
        View coordinatorLayout = findViewById(R.id.coordinatorLayout);
        if (coordinatorLayout == null) {
            Logger.e("cannot find coordinatorLayout");
        } else {
            Snackbar.make(coordinatorLayout, text, Snackbar.LENGTH_SHORT).show();
        }
    }

    /**
     * Toast统一显示入口
     *
     * @param text 显示内容
     */
    protected void toast(CharSequence text) {
        Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
    }

    /**
     * 网络不可用
     */
    protected void netWorkUnable() {
        cancelLoadingDialog();
        snack(getString(R.string.network_unable));
    }

    /**
     * 开启权限授权设置页面
     */
    protected void startPermissionSetting() {
        Intent intent = new Intent();
        intent.setData(Uri.parse("package:" + getPackageName()));
        startActivity(intent);
    }

    /**
     * 隐藏软键盘
     */
    protected void hideKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    /**
     * 显示进度Dialog
     */
    protected void showLoadingDialog() {
        if (loadingDialog == null) {
            loadingDialog = new CProgressDialog(this);
        }
        loadingDialog.show();
    }

    /**
     * 取消进度Dialog
     */
    protected void cancelLoadingDialog() {
        if (loadingDialog != null &&
                loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }
}
